<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2008 Starbugs <univis2typo3@googlegroups.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(PATH_tslib.'class.tslib_pibase.php');
require_once (t3lib_extMgm::extPath('univis2typo3') . 'pi3/class.tx_univis2typo3_pi3.php');
require_once (t3lib_extMgm::extPath('univis2typo3') . 'pi4/class.tx_univis2typo3_pi4.php');
require_once (t3lib_extMgm::extPath('univis2typo3') . 'pi5/class.tx_univis2typo3_pi5.php');


/**
 * Plugin 'personal page' for the 'univis2typo3' extension.
 *
 * @author	Starbugs <univis2typo3@googlegroups.com>
 * @package	TYPO3
 * @subpackage	tx_univis2typo3
 */
class tx_univis2typo3_pi8 extends tslib_pibase {
	var $prefixId      = 'tx_univis2typo3_pi8';		// Same as class name
	var $scriptRelPath = 'pi8/class.tx_univis2typo3_pi8.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'univis2typo3';	// The extension key.

	var $department = '';
	var $personKey  = '';
	var $firstName  = '';
	var $lastName   = '';
	var $personName = '';

	/**
	 * The main method of the PlugIn
	 *
	 * @param	string		$content: The PlugIn content
	 * @param	array		$conf: The PlugIn configuration
	 * @return	The content that is displayed on the website
	 */
	function main($content,$conf)	{
		$this->conf = $conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		$this->pi_USER_INT_obj = 1;	// Configuring so caching is not expected. This value means that no cHash params are ever set. We do this, because it's a USER_INT object!

		$this->department = $this->conf['department_id'];
		$this->templateTotal = $this->cObj->fileResource($this->conf['templateFile']);
		$this->template = $this->cObj->getSubpart($this->templateTotal, "###TEMPLATE_PERSONAL_PAGE###");
		$this->language = $GLOBALS["TSFE"]->sys_language_uid;
		$markers = array();

		$this->loadPersonData();

		// projects
		$markers['###TMPL_PERSONAL_PAGE_PROJECTS###'] = $this->findProjects();

		// publications
		$markers['###TMPL_PERSONAL_PAGE_PUBLICATIONS###'] = $this->findPublications();

		// theses
		$markers['###TMPL_PERSONAL_PAGE_THESES###'] = $this->findTheses();

		// courses
		$markers['###TMPL_PERSONAL_PAGE_LECTURES###'] = $this->findCourses();

		$content = $this->cObj->substituteMarkerArray($this->template, $markers);
		return $content;
	}

	function loadPersonData() {
		// find univis key of page owner
		$select  = '`b`.`tx_univis2typo3_person_key`, `t`.`firstname`, `t`.`lastname`, `b`.`tx_univis2typo3_image`';
		$from    = '`be_users` AS `b` LEFT JOIN `tx_u2t3_person` AS `t` ON `t`.`key` = `b`.`tx_univis2typo3_person_key`';
		$where   = '`b`.`uid` = ' . (int) $GLOBALS['TSFE']->page['perms_userid'];
		$groupby = $orderby = $limit = '';
		$query = $GLOBALS['TYPO3_DB']->SELECTquery(
			$select,
			$from,
			$where,
			$groupby,
			$orderby,
			$limit
		);
		$res =& $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);
		$row =& mysql_fetch_assoc($res);
		$this->personKey  = $row['tx_univis2typo3_person_key'];
		$this->firstName = trim($row['firstname']);
		$this->lastName  = trim($row['lastname']);
		$this->personName = trim($this->firstName . ' ' . $this->lastName);
	}

	function getName() {
	    return $this->personName;
	}

	function getLinkTitle($title) {
	    switch(strtolower($title)) {
		case 'projekte':
		case 'projects':
		    $linkTitle = (1 == $this->language) ? 'Alle %s ansehen, an denen %s arbeitet' : 'View all %s that %s works on';
		    break;
		case 'publikationen':
		case 'publications':
		    $linkTitle = (1 == $this->language) ? 'Alle %s von %s ansehen' : 'View all %s by %s';
		    break;
		case 'theses':
		case 'abschlussarbeiten':
		    $linkTitle = (1 == $this->language) ? 'Alle %s ansehen, die %s betreut(e)' : 'View all %s supervised by %s';
		    break;
		case 'vorlesungen':
		case 'lehrveranstaltungen':
		case 'lectures':
		case 'courses':
		    $linkTitle = (1 == $this->language) ? 'Alle %s von %s ansehen' : 'View all %s held by %s';
		    break;
		default:
		    $linkTitle = (1 == $this->language) ? 'Alle %s von %s ansehen' : 'View all %s of %s';
		    break;
	    }
	    if (0 == $this->language) {
		$title = strtolower($title);
	    }
	    return sprintf($linkTitle, $title, $this->getName());
	}

	function getLinkToPage($title) {
		global $TSFE;
		$query = "SELECT uid,title FROM pages WHERE pid=".intval($TSFE->id).    // Subpages to current page            		
				$this->cObj->enableFields("pages").    							// Include conditions for enableFields           
				" ORDER by sorting";
		$res = mysql(TYPO3_db,$query);    	// Performing query        
		$menuItems=array();        			// Collect each element in this array        
		while($row=mysql_fetch_assoc($res))    {            
			$menuItems[$row["uid"]] = $row["title"];        
		}

		$title_en = $title;
		if (1 == $this->language) {
			switch(strtolower($title)) {
			case 'projekte':
				$title_en = 'projects';
				break;
			case 'publikationen':
				$title_en = 'publications';
				break;
			case 'abschlussarbeiten':
				$title_en = 'theses';
				break;
			case 'lehre':
				$title_en = 'courses';
				break;
			default:
				$title_en = $title;
				break;
			}
		}

		$pageID = array_search(ucfirst(strtolower($title_en)), $menuItems);

		$linkTitle = $this->getLinkTitle($title);

		return $this->pi_linkToPage($linkTitle, $pageID);
	}

	function findProjects() {
		$tmplList  =& $this->cObj->getSubpart($this->templateTotal, '###TMPL_PP_BLOCK_LIST###');
		$tmplItem  =& $this->cObj->getSubpart($this->templateTotal, '###TMPL_PROJECT_ITEM_BRIEF###');
		$title     =  (1 == $this->language) ? 'Projekte' : 'Projects';
		$item_list = '';

		/* Load projects for this person */
		$projects = t3lib_div::makeInstance('tx_univis2typo3_pi3');
		$projects->cObj       =& $this->cObj;
		$projects->department =& $this->department;
		$projects->language   =& $this->language;


		$items = $projects->getNItems($this->personKey, 5);
		if (count($items) < 1) {
		    return '';
		}

		foreach ( $items as $item ) {
		    $markers = array();
		    $markers['###TMPL_PP_PROJ_TITLE###'] = (1 == $this->language) ? $item['title'] : $item['title_en'];
		    $markers['###TMPL_PP_PROJ_TEXT###']  = (1 == $this->language) ? $item['description'] : $item['description_en'];
		    $markers['###TMPL_PP_PROJ_URL###']   = (1 == $this->language) ? $item['url'] : $item['url_en'];
		    $item_list .= $this->cObj->substituteMarkerArray($tmplItem, $markers);
		}

		$markers = array();
		$markers['###TMPL_PP_BLOCK_CLASS###']   = 'person-'.strtolower($title).'-list';
		$markers['###TMPL_PP_BLOCK_HL###']      = $title;
		$markers['###TMPL_PP_BLOCK_ITEMS###']   = $item_list;
		$markers['###TMPL_PP_BLOCK_A_HREF###']  = $this->getLinkToPage($title);
		$markers['###TMPL_PP_BLOCK_A_TITLE###'] = $this->getLinkTitle($title);
		return $this->cObj->substituteMarkerArray($tmplList, $markers);
	}

	function findPublications() {
		$tmplList  =& $this->cObj->getSubpart($this->templateTotal, '###TMPL_PP_BLOCK_LIST###');
		$tmplItem  =& $this->cObj->getSubpart($this->templateTotal, '###TMPL_PUBLICATION_ITEM_BRIEF###');
		$title     =  (1 == $this->language) ? 'Publikationen' : 'Publications';
		$item_list = '';

		/* Load publications for this person */
		$publications = t3lib_div::makeInstance('tx_univis2typo3_pi2');
		$publications->cObj       =& $this->cObj;
		$publications->department =& $this->department;
		$publications->language   =& $this->language;


		$items = $publications->getNItems(null, $this->personKey, 5);
		if (mysql_num_rows($items) < 1) {
		    return '';
		}

		while ( $item = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($items) ) {
		    $markers = array();
		    $markers['###TMPL_PP_PUB_TITLE###'] = $item['pubtitle'];
		    $markers['###TMPL_PP_PUB_TYPE###']  = (1 == $this->language) ? $item['description'] : $item['description_en'];
		    $item_list .= $this->cObj->substituteMarkerArray($tmplItem, $markers);
		}

		$markers = array();
		$markers['###TMPL_PP_BLOCK_CLASS###']   = 'person-'.strtolower($title).'-list';
		$markers['###TMPL_PP_BLOCK_HL###']      = $title;
		$markers['###TMPL_PP_BLOCK_ITEMS###']   = $item_list;
		$markers['###TMPL_PP_BLOCK_A_HREF###']  = $this->getLinkToPage($title);
		$markers['###TMPL_PP_BLOCK_A_TITLE###'] = $this->getLinkTitle($title);
		return $this->cObj->substituteMarkerArray($tmplList, $markers);
	}

	function findTheses() {
		$tmplList  =& $this->cObj->getSubpart($this->templateTotal, '###TMPL_PP_BLOCK_LIST###');
		$tmplItem  =& $this->cObj->getSubpart($this->templateTotal, '###TMPL_THESIS_ITEM_BRIEF###');
		$title     =  (1 == $this->language) ? 'Abschlussarbeiten' : 'Theses';
		$item_list = '';

		/* Load theses for this person */
		$theses = t3lib_div::makeInstance('tx_univis2typo3_pi5');
		$theses->cObj       =& $this->cObj;
		$theses->department =& $this->department;
		$theses->language   =& $this->language;


		$items = $theses->getNItems(null, $this->personKey, 5);
		if (mysql_num_rows($items) < 1) {
		    return '';
		}

		while ( $item = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($items) ) {
		    $markers = array();
		    $markers['###TMPL_PP_THESIS_TITLE###'] = $item['thesis_title'];
		    $markers['###TMPL_PP_THESIS_TYPE###']  = (1 == $this->language) ? $item['thesis_type_description'] : $item['thesis_type_description_en'];
		    $item_list .= $this->cObj->substituteMarkerArray($tmplItem, $markers);
		}

		$markers = array();
		$markers['###TMPL_PP_BLOCK_CLASS###']   = 'person-'.strtolower($title).'-list';
		$markers['###TMPL_PP_BLOCK_HL###']      = $title;
		$markers['###TMPL_PP_BLOCK_ITEMS###']   = $item_list;
		$markers['###TMPL_PP_BLOCK_A_HREF###']  = $this->getLinkToPage($title);
		$markers['###TMPL_PP_BLOCK_A_TITLE###'] = $this->getLinkTitle($title);
		return $this->cObj->substituteMarkerArray($tmplList, $markers);
	}

	function findCourses() {
		$tmplList  =& $this->cObj->getSubpart($this->templateTotal, '###TMPL_PP_BLOCK_LIST###');
		$tmplItem  =& $this->cObj->getSubpart($this->templateTotal, '###TMPL_LECTURE_ITEM_BRIEF###');
		$title     =  (1 == $this->language) ? 'Lehre' : 'Courses';
		$item_list = '';

		/* Load courses for this person */
		$courses = t3lib_div::makeInstance('tx_univis2typo3_pi4');
		$courses->cObj       =& $this->cObj;
		$courses->department =& $this->department;
		$courses->language   =& $this->language;

		$items = $courses->getNItems($this->personKey, 5);
		if (mysql_num_rows($items) < 1) {
		    return '';
		}

		while ( $item = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($items) ) {
		    $markers = array();
		    $markers['###TMPL_PP_LEC_TITLE###'] = $item['name'];
		    //$markers['###TMPL_PP_LEC_TYPE###']  = (1 == $this->language) ? $item['thesis_type_description'] : $item['thesis_type_description_en'];
		    $markers['###TMPL_PP_LEC_URL###'] = $item['url_description'];
		    $item_list .= $this->cObj->substituteMarkerArray($tmplItem, $markers);
		}

		$markers = array();
		$markers['###TMPL_PP_BLOCK_CLASS###']   = 'person-'.strtolower($title).'-list';
		$markers['###TMPL_PP_BLOCK_HL###']      = $title;
		$markers['###TMPL_PP_BLOCK_ITEMS###']   = $item_list;
		$markers['###TMPL_PP_BLOCK_A_HREF###']  = $this->getLinkToPage($title);
		$markers['###TMPL_PP_BLOCK_A_TITLE###'] = $this->getLinkTitle($title);
		return $this->cObj->substituteMarkerArray($tmplList, $markers);
	}
}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/univis2typo3/pi8/class.tx_univis2typo3_pi8.php']) {
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/univis2typo3/pi8/class.tx_univis2typo3_pi8.php']);
}

?>
